package org.iteam.core.service.sys;

import java.sql.SQLException;
import java.util.List;

import org.iteam.core.Constant;
import org.iteam.core.Constant.Function;
import org.iteam.core.Constant.Status;
import org.iteam.core.model.sys.SysFunction;
import org.iteam.core.service.BaseParentServiceInterface;
import org.iteam.core.service.BaseService;
import org.iteam.core.utils.SqlBuilder;

public abstract class SysFunctionService extends BaseService<SysFunction>
		implements BaseParentServiceInterface<SysFunction> {
	private static SysFunctionService serviceMysql = null;
	private static SysFunctionService serviceOracle = null;

	public static SysFunctionService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public static SysFunctionService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new org.iteam.core.service.mysql.impl.sys.SysFunctionServiceImpl();
		return serviceMysql;
	}

	public static SysFunctionService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new org.iteam.core.service.oracle.impl.sys.SysFunctionServiceImpl();
		return serviceOracle;
	}

	public SysFunctionService() {
		super(SysFunction.class);
		// TODO Auto-generated constructor stub
	}

	public long validate(String code) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("code", code);
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase(),
				code).toString());
	}

	public List<SysFunction> queryAll() throws SQLException {
		String sql = "SELECT * FROM SYS_FUNCTION B WHERE STATUS = ? AND TYPE IN (?, ?)";
		return super.query(sql, Status.ENABLE.ecode,
				Function.Type.FUNCTION.ecode, Function.Type.MODULE.ecode);
	}

	public abstract List<SysFunction> queryByPageType(long page, long rows,
			String type, String parent) throws SQLException;

	public abstract List<SysFunction> queryByPageType(long page, long rows,
			String type) throws SQLException;

	public long countByType(String type) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("type", type);
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase(),
				sb.getParams()).toString());
	}

	public long countByType(String type, String parent) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("type", type);
		sb.addWhere("parent", parent);
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase(),
				sb.getParams()).toString());
	}

	@Override
	public List<SysFunction> query() throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		return super.query(sb.getSql().toUpperCase());
	}

	public List<SysFunction> queryAll(String type) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("type", type);
		return super.query(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public List<SysFunction> query(String parent) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("parent", parent);
		return super.query(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public long count(String parent) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		if (parent == null || "".equals(parent))
			sb.addWhere("parent is null");
		else
			sb.addWhere("parent", parent);
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase(),
				sb.getParams()).toString());
	}

	@Override
	public long count() throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase())
				.toString());
	}

	@Override
	public SysFunction show(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysFunction.TABLE_NAME, "*");
		sb.addWhere("code", id);
		return super.show(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean delete(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(SysFunction.TABLE_NAME);
		sb.addField("status", Status.DELETE.ecode);
		sb.addWhere("code", id);
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean destroy(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createDelete(SysFunction.TABLE_NAME);
		sb.addWhere("code", id);
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}
}
